package net.m510.autosurfer.core.process;

import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;

public abstract class AbstractGetProcess<T> extends AbstractSurfProcess<T> {
	protected Logger log = Logger.getLogger(AbstractGetProcess.class.getName());
	
	public AbstractGetProcess() {
		super();
	}

	public AbstractGetProcess(CloseableHttpClient client, HttpClientContext context) {
		super(client, context);
	}
	
	@Override
	public T process(Document doc) throws IOException {
		// リクエスト生成
		HttpGet req = new HttpGet(getUri(doc));
		RequestConfig config = getCustomRequestConfig();
		if ( config != null ) req.setConfig(config);
		// レスポンスハンドラ生成
		ResponseHandler<T> handler = getResponseHandler();
		//　リクエスト送信
		T out = client.execute(req, handler,context);
		
		return out;
	}
	
	protected abstract URI getUri(Document doc);
	protected abstract ResponseHandler<T> getResponseHandler();
	
	protected RequestConfig getCustomRequestConfig() {
		return null;
	}

}
